<template>
  <div class="app-container  bg-gray">
    <transition name="fade-transform" mode="out-in">
      <router-view />
    </transition>
    <el-row v-if="isShow" class="box-1">
      <el-col :span="24">
        <el-card>
          <div class="header">
            <div>
              <el-select v-model="searchTerms.status" placeholder="状态" class="select" @change="getData">
                <el-option :value="1" label="全部"/>
                <el-option :value="2" label="已显示"/>
                <el-option :value="3" label="已隐藏"/>
              </el-select>
              <el-input v-model="searchTerms.searchValue" placeholder="请输入内容" class="search-input" @change="getData" />
              <el-button type="success" class="do-btn" icon="el-icon-search" @click="getData">搜索</el-button>
              <div class="flex-1" />
              <el-button v-if="queryMatch(46)" type="success" class="bg-green" icon="el-icon-circle-plus" @click="toModify('新增新闻')">新增新闻</el-button>
            </div>
          </div>
          <el-table v-loading="tableLoading" :data="tableData" :max-height="tableMaxHeight" border highlight-current-row @sort-change="handleSortChange">
            <el-table-column prop="id" label="ID" min-width="80" header-align="center" align="center" sortable />
            <el-table-column label="缩略图" width="130" header-align="center" align="center">
              <template slot-scope="scope">
                <el-image :src="scope.row.thumb||defaultAvatar" fit="contain" class="cursor-p" style="width: 90px;height: 60px;" @click="toInfo(scope.row)"/>
              </template>
            </el-table-column>
            <el-table-column prop="title" label="新闻标题" min-width="260">
              <template slot-scope="scope">
                <div class="cursor-p" @click="toInfo(scope.row)">{{ scope.row.title }}</div>
              </template>
            </el-table-column>
            <el-table-column prop="type_name" label="新闻分类" min-width="140" header-align="center" align="center"/>
            <el-table-column prop="created_at" label="发布时间" min-width="160" header-align="center" align="center" sortable/>
            <el-table-column v-if="queryMatch(47)" prop="read_power" label="查看权限" min-width="120" header-align="center" align="center">
              <template slot-scope="scope">
                {{ scope.row.read_power===0?'全部':scope.row.read_power===1?'内部':'外部' }}
              </template>
            </el-table-column>
            <el-table-column v-if="queryMatch(47)" label="状态" min-width="80" header-align="center" align="center">
              <template slot-scope="scope">
                <div @click="queryMatch(47)?setState(scope.row):''">
                  <el-tag :type="scope.row.is_display==1?'danger':'success'">{{ scope.row.is_display==1?'已隐藏':'已显示' }}</el-tag>
                </div>
              </template>
            </el-table-column>
            <el-table-column v-if="queryMatch(47)" label="操作" min-width="200" header-align="center" align="center">
              <template slot-scope="scope">
                <el-button v-if="queryMatch(47)" type="success" class="bg-green" size="small" icon="el-icon-edit" circle @click="toModify('编辑新闻', scope.row)" />
                <el-button v-if="queryMatch(48)" type="danger" size="small" icon="el-icon-delete" circle @click="toDelete(scope.row)" />
              </template>
            </el-table-column>
          </el-table>
          <el-pagination
            :current-page.sync="currentPage"
            :page-size="pageSize"
            :page-sizes="pageSizes"
            :total="total"
            class="pagination"
            layout="total, sizes, prev, pager, next, jumper"
            @current-change="handleCurrentChange"
            @size-change="handleSizeChange"/>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import defaultAvatar from '../../../images/default_avatar.png'
import { handleSortChange, handleSizeChange, handleCurrentChange } from '../../../utils/table'

export default {
  data() {
    return {
      defaultAvatar: defaultAvatar,
      isShow: false,
      searchTerms: {
        searchValue: '',
        status: 1
      },
      // 文章類型id
      newsTypeId: '',

      // 表格相关数据
      tableMaxHeight: document.documentElement.clientHeight - 280,

      // 表格相关参数
      tableLoading: false,
      currentPage: 1,
      pageSize: this.$store.getters.userInfo.pageSize,
      pageSizes: [10, 20, 50, 100, 1000],
      total: 0,
      tableData: []
    }
  },
  watch: {
    $route(to) {
      if (to.name === '新闻列表') {
        this.getData()
        setTimeout(() => {
          this.isShow = true
        }, 500)
      } else {
        this.isShow = false
      }
    }
  },
  created() {
    this.newsTypeId = this.$route.query.id
    if (this.$route.name === '新闻列表') {
      this.isShow = true
      this.getData()
    }
  },

  methods: {
    handleCurrentChange, handleSizeChange, handleSortChange,
    getData(order) {
      this.tableLoading = true
      order || (order = {})
      const url = 'article/list'
      const data = {
        typeid: 1,
        page_no: this.currentPage,
        page_size: this.pageSize,
        search: this.searchTerms.searchValue,
        articles_type_id: this.newsTypeId,
        status: this.searchTerms.status
      }
      order.sortName && (data.sortName = order.sortName)
      order.sortOrder && (data.sortOrder = order.sortOrder)
      this.$store.dispatch('GetConnect', { url, data }).then(res => {
        this.tableData = res.data.rows
        this.total = res.data.total
        this.tableLoading = false
      }).catch(e => {
        this.tableLoading = false
        this.$message.error(e.msg + ',请刷新或联系管理员')
      })
    },

    toInfo(row) {
      this.isShow = false
      this.$router.push({
        path: '/information/list/info',
        query: { id: row.id }
      })
    },

    /**
       * 设置当前新闻的状态
       * row: 当前点击的行的数据
       */
    setState(row) {
      row.is_display = row.is_display === 0 ? 1 : 0
      const url = 'article/ajax/' + row.id
      const data = { action: 'status', is_display: row.is_display }
      this.$store.dispatch('GetConnect', { url, data }).then(res => {
        this.$message.success('修改新闻状态成功，状态：' + (row.is_display === 0 ? '已显示' : '已隐藏'))
      }).catch(e => {
        row.is_display = row.is_display === 0 ? 1 : 0
        this.$message.error('修改新闻状态失败，请刷新或联系管理员')
      })
    },

    /**
       * 删除产品
       * row: 当前点击的行的数据
       */
    toDelete(row) {
      this.$confirm('此操作将把该新闻删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const url = 'article/delete/' + row.id
        this.$store.dispatch('GetConnect', { url }).then(res => {
          this.$message.success(res.msg)
          this.getData()
        }).catch(e => {
          this.$message.error('删除失败,请刷新或联系管理员!')
        })
      })
    },

    /**
       * 跳转产品修改页面
       * title: 新增/修改
       * row: 当前点击的行的数据,为空时为新增
       */
    toModify(title, row) {
      this.isShow = false
      const query = { title: title, typeId: this.newsTypeId }
      row && (query.id = row.id)
      this.$router.push({
        path: '/information/list/modify',
        query: query
      })
    }
  }
}
</script>

<style lang="scss" type="text/scss" scoped>
@import '../../../styles/index.scss';
.box-1 {
  .header {
    @include flex-row(center, space-between);
    padding-bottom: 15px;
    flex-wrap: wrap;
    > div {
      @include flex-row(center, flex-start);
      flex-wrap: wrap;
      width: 100%;
    }
    .select{
      width: 140px;
      margin-right: 10px;
    }
    .search-input {
      width: 240px;
    }
    .do-btn {
      @extend .bg-green;
      margin-left: 6px;
    }
  }
}
.el-tag {
  cursor: pointer;
}
</style>
